package org.ht.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.azj.dbOperatorComponent.ICommand;
import org.azj.dbOperatorComponent.IDbNonQueryOperation;
import org.azj.dbOperatorComponent.IDbQueryOperation;
import org.azj.dbOperatorComponent.IJobManager;
import org.azj.dbOperatorComponent.OperableTable;
import org.azj.dbOperatorComponent.SelectFilter;
import org.azj.dbOperatorComponent.TransactionHelper;
import org.ht.config.ConstField;
import org.ht.database.DataBase;
import org.ht.entity.Case;
import org.ht.global.DbTableNames;
import org.ht.util.CaseUtil;

/**
 * 控制病例的增删改查
 * 
 * @author HT
 * 
 */
public class ControlCase {

	// 根据病历号查询这个Case实体
	public Case queryCase(String pid) throws Exception {
		Case thecase = new Case();

		String where = ConstField.PID + " = '" + pid + "'";
		ICommand command;
		IJobManager jobManager = new TransactionHelper(DataBase.getDataBase());

		// 查询基本信息
		command = generateQueryCommands(where, DbTableNames.FullTblBasicInfo);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblBeforeOp);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblMidOp);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblPostOp);
		jobManager.addJob(command);

		List<List<HashMap<String, Object>>> resultList = MainController
				.doJobsInTransaction(jobManager);
		// 事务处理失败就返回
		if (resultList == null) {
			return thecase;
		}
		// 设置4张表
		CaseUtil.fillCase(thecase.getBasicInfo().property, resultList.get(0)
				.get(0));
		CaseUtil.fillCase(thecase.getOperationBefore().property, resultList
				.get(1).get(0));
		CaseUtil.fillCase(thecase.getOperationMiddle().property, resultList
				.get(2).get(0));
		CaseUtil.fillCase(thecase.getOperationAfter().property,
				resultList.get(3).get(0));
		return thecase;
	}

	// 根据病历号查询这些Case实体
	public List<Case> queryCases(List<String> pids) throws Exception {
		StringBuffer whereBuffer = new StringBuffer(ConstField.PID + " IN (");
		for (String pid:pids){
			whereBuffer.append("'"+pid+"',");
		}
		whereBuffer.deleteCharAt(whereBuffer.length()-1);
		whereBuffer.append(")");
		String where = whereBuffer.toString();
		ICommand command;
		IJobManager jobManager = new TransactionHelper(DataBase.getDataBase());

		// 查询基本信息
		command = generateQueryCommands(where, DbTableNames.FullTblBasicInfo);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblBeforeOp);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblMidOp);
		jobManager.addJob(command);

		command = generateQueryCommands(where, DbTableNames.FullTblPostOp);
		jobManager.addJob(command);

		List<List<HashMap<String, Object>>> resultList = MainController
				.doJobsInTransaction(jobManager);
		// 事务处理失败就返回
		if (resultList == null) {
			return null;
		}
		// 设置4张表
		HashMap<String,Case> caseMap = new HashMap<String,Case>();
		// 构建哈希表
		for (HashMap<String, Object> thecase:resultList.get(0)){
			Case mycase = new Case();
			String pid = getPid(DbTableNames.FullTblBasicInfo,thecase);
			caseMap.put(pid, mycase);
		}
		
		// 第一张表填充
		for (HashMap<String, Object> thecase:resultList.get(0)){
			String pid = getPid(DbTableNames.FullTblBasicInfo,thecase);
			CaseUtil.fillCase(caseMap.get(pid).getBasicInfo().property,thecase);
		}
		// 第二张表填充
		for (HashMap<String, Object> thecase:resultList.get(1)){
			String pid = getPid(DbTableNames.FullTblBeforeOp,thecase);
			CaseUtil.fillCase(caseMap.get(pid).getOperationBefore().property,thecase);
		}
		// 第三张表填充
		for (HashMap<String, Object> thecase:resultList.get(2)){
			String pid = getPid(DbTableNames.FullTblMidOp,thecase);
			CaseUtil.fillCase(caseMap.get(pid).getOperationMiddle().property,thecase);
		}
		// 第四张表填充
		for (HashMap<String, Object> thecase:resultList.get(3)){
			String pid = getPid(DbTableNames.FullTblPostOp,thecase);
			CaseUtil.fillCase(caseMap.get(pid).getOperationAfter().property,thecase);
		}
		List<Case> result= new ArrayList<Case>();
		result.addAll(caseMap.values());
		return result;
	}
	
	/**
	 * 根据PID删除病例
	 * 
	 * @param pid
	 * @return
	 * @throws Exception
	 */
	public boolean deleteCase(String pid) throws Exception {
		if (pid == null || pid.isEmpty()) {
			return false;
		}
		String where = ConstField.PID + " = '" + pid + "'";
		ICommand command;
		IJobManager jobManager = new TransactionHelper(DataBase.getDataBase());

		// 删除基本信息
		command = generateDeleteCommands(where, DbTableNames.FullTblBasicInfo);
		jobManager.addJob(command);

		// 删除术前信息
		command = generateDeleteCommands(where, DbTableNames.FullTblBeforeOp);
		jobManager.addJob(command);

		// 删除术中信息
		command = generateDeleteCommands(where, DbTableNames.FullTblMidOp);
		jobManager.addJob(command);

		// 删除术后信息
		command = generateDeleteCommands(where, DbTableNames.FullTblPostOp);
		jobManager.addJob(command);

		// 删除图片信息
		command = generateDeleteCommands(where, DbTableNames.fullTblImage);
		jobManager.addJob(command);

		// 事务处理失败就返回
		if (null == MainController.doJobsInTransaction(jobManager)) {
			return false;
		}
		return true;
	}

	/**
	 * 生成删除接口
	 * 
	 * @param where
	 *            where语句
	 * @param tableFullName
	 *            表的全名
	 * @return 接口
	 * @throws Exception
	 */
	private ICommand generateDeleteCommands(String where, String tableFullName)
			throws Exception {
		String tableName = tableFullName.substring(tableFullName
				.lastIndexOf('.') + 1);
		IDbNonQueryOperation tableUnit = new OperableTable(
				DataBase.getDataBase(), DbTableNames.SchemaName, tableName);
		return tableUnit.getDeleteCommand(where);
	}

	/**
	 * 生成查询接口
	 * 
	 * @param where
	 *            where语句
	 * @param tableFullName
	 *            表的全名
	 * @return 接口
	 * @throws Exception
	 */
	private ICommand generateQueryCommands(String where, String tableFullName)
			throws Exception {
		String tableName = tableFullName.substring(tableFullName
				.lastIndexOf('.') + 1);
		IDbQueryOperation tableUnit = new OperableTable(DataBase.getDataBase(),
				DbTableNames.SchemaName, tableName);
		SelectFilter filter = new SelectFilter();
		filter.setWhereFilter(where);
		return tableUnit.getSelectCommand(filter);
	}
	
	/**
	 * @param thecase
	 * @return
	 */
	private String getPid(String tableName, HashMap<String, Object> thecase){
		return thecase.get((tableName+"."+ConstField.PID).toUpperCase()).toString();
		
	}
}
